﻿using Core;
using Net.Web.Models.Common;
using Core.Domain.Customers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Services.Topics;
using Services.Catelog;
using Services.Seo;

namespace Net.Web.Controllers
{
    /// <summary>
    /// 公共控制器
    /// </summary>
    public class CommonController : BaseController
    {
        #region Fields

        private readonly ITopicService _topicService;
        private readonly ICategoryService _categoryService;
        private readonly IFriendlyLinkService _friendlyLinkService;
        private readonly IWorkContext _workContext;
        private readonly IStoreContext _storeContext;
        private readonly IWebHelper _webHelper;

        #endregion

        #region Con
        public CommonController(
            IWorkContext workContext,
            IStoreContext storeContext,
            IWebHelper webHelper,
            ITopicService topicService,
            ICategoryService cateogyrService,
            IFriendlyLinkService friendlyLinkService)
        {
            this._workContext = workContext;
            this._storeContext = storeContext;
            this._webHelper = webHelper;
            this._topicService = topicService;
            this._categoryService = cateogyrService;
            this._friendlyLinkService = friendlyLinkService;
        }
        #endregion

        #region Action

        [ChildActionOnly]
        /// <summary>
        /// 头文件
        /// </summary>
        /// <returns></returns>
        public ActionResult HeaderLinks()
        {
            var customer = _workContext.CurrentCustomer;

            var model = new HeaderLinksModel()
            {
                IsAuthenticated = customer.IsRegistered(),
                IsApplicant = customer.RegisterEnum == RegisterEnum.Applicant,
                IsCompany = customer.RegisterEnum == RegisterEnum.Company,
                IsHeadHunter = customer.RegisterEnum == RegisterEnum.HeadHunter,
                Name = customer.Username,
                Email = customer.Email,
            };
            return PartialView(model);
        }

        /// <summary>
        /// 错误页面
        /// </summary>
        /// <returns></returns>
        public ActionResult PageNotFound()
        {
            this.Response.StatusCode = 404;
            this.Response.TrySkipIisCustomErrors = true;

            return View();
        }

        /// <summary>
        /// 脚注信息
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult Footer()
        {

            var topics = _topicService.GetAllTopics(0, 10);
            var top = (from p in topics
                       select new SelectListItem
                       {
                           Disabled = p.Active,
                           Selected = false,
                           Text = p.Title,
                           Value = p.SystemName,
                       }).ToList();
            var model = new FooterModel();
            model.Topic = top;
            return PartialView(model);
        }

        /// <summary>
        /// 首页类别
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult HomeCatalog()
        {
            var categorys = _categoryService.GetAllCategories(pageIndex: 0, pageSize: 10);
            var model = new HomeCatalogModel();
            model.Category = (from p in categorys
                              select new SelectListItem
                              {
                                  Text = p.Name,
                                  Disabled = p.ParentId == 0,
                                  Value = p.SeName,
                              }).ToList();
            return PartialView(model);
        }

        /// <summary>
        /// 脚注友情链接
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult FooterFriendlyLink()
        {
            var list = _friendlyLinkService.GetFriendlyLinks(top: true, pageSize: 30, pageIndex: 0);
            var model = new FooterFriendlyLinkModel();

            var flinks = (from p in list
                          select new SelectListItem
                          {
                              Text = p.Name,
                              Value = p.Url
                          }).ToList();
            model.FriendlyLinks = flinks;
            return PartialView(model);
        }
        #endregion
    }
}